package element;

import game.GamePanel;

import java.awt.*;
import java.awt.event.MouseEvent;

import static utils.Constant.*;

/**
 * @author 邓哈哈
 * 2023/2/19 23:37
 * Function: 红线
 * Version 1.0
 */

public class Line {
    //定义界面类
    GamePanel gamePanel;
    //定义起始坐标
    int x;
    int y;
    //定义终点坐标
    int end_x;
    int end_y;
    //定义线长
    int length;
    //定义线转动频率
    double angle;
    //定义转动方向
    boolean direction;
    //定义红线状态(0-摇摆 1-延长 2-收缩 3-抓取收缩)
    int state;
    //定义勾爪
    Image hook;

    /**
     * 初始化
     */
    public Line(GamePanel gamePanel) {
        this.gamePanel = gamePanel;
        //初始化起点坐标
        this.x = 380;
        this.y = 180;
        //初始化线长
        this.length = 100;
        //初始化线转动频率
        this.angle = 0;
        //初始化转动方向(顺时针方向)
        this.direction = true;
        //初始化红线状态(摇摆)
        this.state = 0;
        //初始化狗爪
        this.hook = Toolkit.getDefaultToolkit().getImage("src/main/imgs/hook.png");
    }

    public void paintSelf(Graphics g) {
        //抓取判定
        this.logic();
        //根据红线不同状态, 调整红线的长度, 角度, 方向
        switch (this.state) {
            //摇摆状态
            case 0:
                //初始化红线角度
                if (angle < 0.1)//限制右高度, 改顺时针
                    this.direction = true;
                else if (angle > 0.9)//限制左高度, 改逆时针
                    this.direction = false;
                //根据方向, 调整角度
                if (direction)
                    angle += 0.005;
                else
                    angle -= 0.005;
                drawLine(g);
                break;
            //延长状态
            case 1:
                if (this.length < MAX_LENGTH)//延长
                    this.length += MAX_SPEED;
                else
                    this.state = 2;//切换收缩状态
                drawLine(g);
                break;
            //收缩状态
            case 2:
                if (this.length > MIN_LENGTH)
                    this.length -= MAX_SPEED;//收缩
                else
                    this.state = 0;//切换摇摆状态
                drawLine(g);
                break;
            //抓取收缩
            case 3:
                int m = 0;
                if (this.length > MIN_LENGTH) {
                    //收缩
                    this.length -= MIN_SPEED;
                    drawLine(g);

                    for (MyObject myObject : gamePanel.getMyObjectList()) {
                        //只有方块存活时
                        if (myObject.alive) {
                            //获取方块质量
                            m = myObject.quality;
//                            System.out.println(m);
                            //方块的坐标跟随红线末端坐标
                            myObject.x = this.end_x - myObject.width / 2;
                            myObject.y = this.end_y;

                            //使用药水 && 抓取了石头
                            if (WATER_FLAG && myObject.getType() == 2) {
                                //红线收缩
                                this.state = 2;
                                //方块消失
                                myObject.x = -150;
                                myObject.y = -150;
                                //方块置为未存活
                                myObject.alive = false;
                                //积分不变
                                //药水状态置为未使用
                                WATER_FLAG = false;
                            }

                            //使用药水 && 抓取了金块
                            else if (WATER_FLAG && myObject.getType() == 1) {
                                //减少质量, 加快红线速度
                                m = 0;
                            }

                            //正常抓取收回
                            if (this.length <= MIN_LENGTH) {
                                //红线摇摆
                                this.state = 0;
                                //方块消失
                                myObject.x = -150;
                                myObject.y = -150;
                                //方块置为未存活
                                myObject.alive = false;
                                //积分
                                COUNT += myObject.count;
                                //药水状态置为未使用
                                WATER_FLAG = false;
                            }
                        }
                    }
                }
                try {
                    Thread.sleep(m);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                break;
            default:
                break;
        }
    }

    /**
     * 绘制红线
     *
     * @param g
     */
    public void drawLine(Graphics g) {
        //初始化终点坐标
        this.end_x = (int) (x + length * Math.cos(this.angle * Math.PI));
        this.end_y = (int) (y + length * Math.sin(this.angle * Math.PI));
        //设置画笔颜色
        g.setColor(Color.RED);
        //画红线
        g.drawLine(x - 1, y - 1, end_x - 1, end_y - 1);
        g.drawLine(x, y, end_x, end_y);
        g.drawLine(x + 1, y + 1, end_x + 1, end_y + 1);
        //画钩爪
        g.drawImage(this.hook, this.end_x - 36, this.end_y - 2, null);
    }

    /**
     * 抓取判定
     */
    public void logic() {
        for (MyObject myObject : gamePanel.getMyObjectList()) {
            //判断红线末端与方块是否相交
            if (this.end_x > myObject.x
                    && this.end_x < myObject.x + myObject.width
                    && this.end_y > myObject.y
                    && this.end_y < myObject.y + myObject.height) {
                //红线状态改为抓取收缩
                this.state = 3;
                //被抓取方块设为存活
                myObject.alive = true;
                System.out.println("中");
            }
        }
    }

    /**
     * 监听鼠标点击
     *
     * @param e
     */
    public void mouseClicked(MouseEvent e) {
        //1 鼠标左键点击 2 鼠标中键点击 3 鼠标右键点击
        //红线摇摆 鼠标左键
        if (this.state == 0 && e.getButton() == 1) {//1
            this.state = 1;//切换延长状态
            //红线抓取收缩 鼠标右键 药水数量足够
        } else if (this.state == 3 && e.getButton() == 3 && WATER_COUNT > 0 && !WATER_FLAG) {
            //开启使用药水
            WATER_FLAG = true;
            //药水数量减一
            WATER_COUNT--;
        }
    }
}
